import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'multi_state_bloc.dart';

class SubWidget extends StatefulWidget {
  final MultiStateBloc bloc;

  const SubWidget({Key key, this.bloc}) : super(key: key);

  @override
  State<SubWidget> createState() => _SubWidgetState();
}

class _SubWidgetState extends State<SubWidget> {
  @override
  Widget build(BuildContext context) {
    // 子widget，这里就不是create了，而是.value
    return BlocProvider.value(
      // value必传
      value: widget.bloc,
      child: BlocBuilder(
        // cubit or bloc必传
        cubit: widget.bloc,
        builder: (context, state) {
          if (state is PageViewState || state is DataState) {
            String data = "";
            if (state is DataState) {
              data = state?.data ?? "-";
            } else {
              data = "PageViewState";
            }
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text("data is: ${data}"),
                MaterialButton(
                  onPressed: () {
                    widget.bloc?.add(DataChangeEvent2("data!"));
                  },
                  child: Text("change data event2"),
                ),
              ],
            );
          } else {
            return Text("no such state");
          }
        },
      ),
    );
  }
}
